package win.larryzeal.thymeleaf.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import win.larryzeal.thymeleaf.entity.Author;
import win.larryzeal.thymeleaf.entity.Book;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by 张少昆 on 2017/10/20.
 */
@Controller
@RequestMapping( "/basic" )
public class BasicController {
	Set<String> set = new HashSet<>();

	{
		set.add("sharp");//#
		set.add("dollar");//$
		set.add("star");//*
		set.add("at");//@

		set.add("if");//<div th:if="${  }" ></div>
	}

	@RequestMapping( value = {"/{demo}"} )
	public String sharp(@PathVariable( "demo" ) String demo, Map map){
		// ${..} #{..} *{..} @{..}
		if(set.contains(demo)){
			map.put("user", createAuthor());
			map.put("book", createBook());
			map.put("books", createBooks(3));
		}

		return "basic/" + demo;
	}

//************************************************************************
	private Author createAuthor(){
		Author author = new Author();
		author.setName("Larry");
		author.setAge(18);
		author.setSex(true);
		return author;
	}

	private Book createBook(){
		Book book = new Book();
		book.setAuthor(createAuthor()); //
		book.setName("Larry's Talk");
		book.setPrice(99.99);
		book.setSn("157-999-sn");
		return book;
	}

	private List<Book> createBooks(int num){
		List<Book> list = new ArrayList<>(num);
		for(int i = 0; i < num; i++){
			Book book = createBook();
			book.setPrice(99.99 + i);
			book.setSn("157-999-sn-" + i);
			list.add(book);
		}
		return list;
	}
}
